import Vue from 'vue'
import Router from 'vue-router'

//批量读取模块文件
const files = require.context('./modules', true, /\.js$/);

const modules = [];
files.keys().forEach(path => {
  const aModule = files(path);
  modules.push(...aModule.default);
});

const originalReplace = Router.prototype.replace;
Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};
Vue.use(Router);

const router = new Router({ 
  mode: 'history',
  base: 'limit',
  routes: [
    ...modules,
  ],
  scrollBehavior(to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  },
});
export default router;
